package com.admission.web;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Academy;
import com.admission.model.Major;
import com.admission.service.AcademyManager;
import com.admission.service.MajorManager;
import com.admission.util.FileUtils;
import com.admission.util.PageUtils;
import com.opensymphony.xwork2.ActionContext;
@Namespace("/base")
@Results({@Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "redirect") })
public class MajorAction extends CrudActionSupport<Major>{

	private static final long serialVersionUID = 488185923443490311L;
	private String id;
	private String academyId; //学院类的Id
	private Major entity;
	private Academy academy;
	//private Page<Academy> page = new Page<Academy>(PageUtils.DEFAULT_PAGE_SIZE);
	private Page<Major> page = new Page<Major>(PageUtils.DEFAULT_PAGE_SIZE);
    private MajorManager majorManager;
    private String keyword; //关键字查找
    private List<Major> majorlist;
    private AcademyManager academyManager;
    private List<Academy> academyList;
    
	public Major getModel() {
		return entity;
	}
   /**
    * 通过academyId查找
    * @return List<Major>
    */
	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p=new PropertyFilter("EQS_academy.id",academyId );
		filters.add(p);
		page=majorManager.getPageByFilter(page, filters);
		majorlist=page.getResult();
		academy=page.getResult().get(0).getAcademy();
		return "list";
	}
	/**
	    * 通过majorkeyword查找
	    * @return List<major>
	    */
		public String byMajorKeyword() throws Exception {
			List<PropertyFilter> filters = PropertyFilter
					.buildFromHttpRequest(Struts2Utils.getRequest());
			//PropertyFilter p=new PropertyFilter("EQS_id",id);
			PropertyFilter  p=new PropertyFilter("LIKES_name",keyword);
			filters.add(p);
			page=majorManager.getPageByFilter(page, filters);
			return "search";
		}
	
		/**
	    * 通过keyword查找
	    * @return List<major>
	    */
		public String byKeyword() throws Exception {
			List<PropertyFilter> filters = PropertyFilter
					.buildFromHttpRequest(Struts2Utils.getRequest());
			PropertyFilter p=null;
			if(!(id.equals("")||id==null)){
				   p=new PropertyFilter("EQS_id",id);
				}
			else{
			   p=new PropertyFilter("EQS_academy.id",academyId );
			}
			filters.add(p);
			page=majorManager.getPageByFilter(page, filters);
			if(page.getResult().size()==1){
				id=page.getResult().get(0).getId();
			  return input();
			}
			return "search";
		}
	   
	@Override
	public String input() throws Exception {
		List<PropertyFilter> filters2 = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p=new PropertyFilter("EQS_state","1");
		filters2.add(p);
		academyList=academyManager.getListByFilter(filters2);
		entity=majorManager.getMajorById(id);
		return "detail";
	}
	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		 majorManager.save(entity, academyId);
		 return null;	
	}


	//级联
	public String majorlist() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter p = new PropertyFilter("EQS_academy.id", academyId);
		filters.add(p);
		List<Major> majors = majorManager.getListByFilter(filters);
		JSONArray majorArray = new JSONArray();
		JSONObject jo = null;
		//设置需要被格式化为JSON字符串的内容.
		for(Major major : majors){
			jo = new JSONObject();
			jo.put("id", major.getId());
			jo.put("name", major.getName());
			majorArray.put(jo);
		}
		// 渲染返回结果.
		Struts2Utils.renderJson(majorArray.toString());
		return null;
	}
	// 异步查询访问人数
		public String visitorCount() throws IOException {
			String path = ServletActionContext.getServletContext().getRealPath(
					"/WEB-INF");
			String r = "";
			path += "/other/visitCount.properties";
			File f = new File(path);
			if (f.exists()) {
				f.createNewFile();
			}
			ActionContext actionContext = ActionContext.getContext();
			Map session = actionContext.getSession();
			if (session.get(id) == null) {
				r = FileUtils.getProperties(path, id, "1", true);
				session.put(id, "1");
			} else {
				r = FileUtils.getProperties(path, id, "1", false);
			}

			Struts2Utils.renderText(r);
			return null;
		}
	@Override
	public String delete() throws Exception {
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(StringUtils.isEmpty(id)){
			entity = new Major();
		}else{
			entity = majorManager.getMajorById(id);
		}
		
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Major getEntity() {
		return entity;
	}

	public void setEntity(Major entity) {
		this.entity = entity;
	}

	public Page<Major> getPage() {
		return page;
	}

	public void setPage(Page<Major> page) {
		this.page = page;
	}

	public MajorManager getMajorManager() {
		return majorManager;
	}
    @Autowired
	public void setMajorManager(MajorManager majorManager) {
		this.majorManager = majorManager;
	}

	public String getAcademyId() {
		return academyId;
	}

	public void setAcademyId(String academyId) {
		this.academyId = academyId;
	}
	public Academy getAcademy() {
		return academy;
	}
	public void setAcademy(Academy academy) {
		this.academy = academy;
	}
	public String getKeyword() {
		return keyword;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	public List<Major> getMajorlist() {
		return majorlist;
	}
	public void setMajorlist(List<Major> majorlist) {
		this.majorlist = majorlist;
	}
	public AcademyManager getAcademyManager() {
		return academyManager;
	}
	public void setAcademyManager(AcademyManager academyManager) {
		this.academyManager = academyManager;
	}
	public List<Academy> getAcademyList() {
		return academyList;
	}
	public void setAcademyList(List<Academy> academyList) {
		this.academyList = academyList;
	}
    
   
   
}
